package com.jingdun.sport.sporttype

import com.losttemple.sql.language.operator.*
import com.losttemple.sql.language.types.SetRef
import com.losttemple.sql.language.types.SqlType

class SportTypes(set: (TableConfigure.()->Unit)-> SetRef) :
        DbSource(set { tableName("sport_types") }) {
    var id = intColumn("id")
    var parentId = intColumn("parent_id")
    var currentConfig = intColumn("current_config")
    var enabled = boolColumn("enabled")
}

class SportTypeRelations(set: (TableConfigure.()->Unit)-> SetRef) :
        DbSource(set { tableName("sport_type_relations") }) {
    var id = intColumn("id")
    var ancestor = intColumn("ancestor")
    var child = intColumn("child")
}

class SportSurfaces(set: (TableConfigure.()->Unit)-> SetRef) :
        DbSource(set { tableName("sport_surface") }) {
    var id = intColumn("id")
    var surface = stringColumn("surface")
    var sportType = intColumn("sport_type")
    var enabled = boolColumn("enabled")
}

class SportConfig(set: (TableConfigure.()->Unit)-> SetRef) :
        DbSource(set { tableName("sport_config") }) {
    var id = super.intColumn("id")
    var type = intColumn("type")
    var name = stringColumn("name")
    var description = stringColumn("description")
    var supportTiming = boolColumn("support_timing")
    var supportCounting = boolColumn("support_counting")
    var trainVideo = stringColumn("train_video")
    var introduceVideo = stringColumn("introduce_video")
    var mainLogo = stringColumn("main_logo")
    var lastConfig = intColumn("last_config")
}

data class SportDetail(
        val id: SqlType<Int>,
        val enabled: SqlType<Boolean>,
        var parentId:SqlType<Int>,
        val config: SportConfig
)

fun sportTypeDetail(): DbSet<SportDetail> {
    return (from { SportTypes(it) } innerJoin from { SportConfig(it) })
            .on { type, config -> type.currentConfig eq config.id }
            .select { type, config ->
                SportDetail(
                    type.id,
                    type.enabled,
                    type.parentId, 
                    config
                )
            }
}